package ee.siimkaspar.subjects.validator;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import ee.siimkaspar.subjects.model.*;

public class SubjectValidator {
	/**
	 * Use this person validator when adding persons
	 * @param person
	 * @return
	 */
	public HashMap<String, String> validatePerson(Person person){
		Calendar now = Calendar.getInstance();
		Calendar birthdate = Calendar.getInstance();
		birthdate.set(person.getBirthDate().getYear()+1900, person.getBirthDate().getMonth(), person.getBirthDate().getDate());
		HashMap<String, String> errors = new HashMap<String, String>();
		if(person.getFirstName().length() < 1 || person.getFirstName().length() > 40){
			errors.put("firstNameError", "Person's firstname must be between 1 and 40 characters");
		}
		if(person.getLastName().length() < 1 || person.getLastName().length() > 40){
			errors.put("lastNameError", "Person's lastname must be between 1 and 40 characters");
		}
		if(now.compareTo(birthdate) == -1){
			errors.put("birthdateError", "Person's birth date cannot be in the future");
		}
		return errors;
	}
	/**
	 * use this enterprise validator when adding enterprises
	 * @param enterprise
	 * @return
	 */
	public HashMap<String, String> validateEnterprise(Enterprise enterprise){
		HashMap<String, String> errors = new HashMap<String, String>();
		if(enterprise.getFullName().length() < 1 || enterprise.getFullName().length() > 255){
			errors.put("fullNameError", "Enterprise's fullname must be between 1 and 255 characters");
		}
		if(enterprise.getName().length() < 1 || enterprise.getName().length() > 255){
			errors.put("nameError", "Enterprise's name must be between 1 and 255 characters");
		}
		return errors;
	}
	public HashMap<String, String> validatePersonDelete(Person person){
		HashMap<String, String> errors = new HashMap<String, String>();
		if(person.getEnterprisePersonRelations().size() > 0){
			errors.put("personEnterpriseRelatsionError", "Person is tied to enterprise. Unable to delete.");
		}		
		return errors;
	}
	public HashMap<String, String> validateAddress(Address address){
		HashMap<String, String> errors = new HashMap<String, String>();
		if(address.getCountry().length() < 1 || address.getCountry().length() > 50){
			errors.put("countryError", "Country's name must be between 1 to 50 characters.");
		}
		if(address.getCounty().length() < 1 || address.getCounty().length() > 100){
			errors.put("countyError", "County's name must be between 1 to 100 characters.");
		}
		if(address.getTownVillage().length() < 1 || address.getTownVillage().length() > 100){
			errors.put("townVillageyError", "Town/village's name must be between 1 to 100 characters.");
		}
		if(address.getStreetAddress().length() < 1 || address.getStreetAddress().length() > 100){
			errors.put("streetAddressError", "Street address must be between 1 to 100 characters.");
		}
		return errors;
	}
	public SubjectValidator(){}
}
